<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<script src="/plugins/bootstrap-fileinput/js/fileinput.min.js" type="text/javascript"></script>
	<link href="/plugins/bootstrap-fileinput/css/fileinput.min.css" type="text/css" rel="stylesheet">
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="delContent()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="contentBody">
			                          <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" width="100px" height="50px">
									      </td>
									      <td>1</td>
									      <td>有效</td>									     								     
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 新增窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告新增</h3>
		</div>
		<div class="modal-body">							
			<form id="addForm">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" name="categoryId" id="categorySelect">
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" name="title" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" name="url" placeholder="URL" >  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" name="sortOrder" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<input type="hidden" id="image" name="pic">
									<input type="file" id="file" name="myFile" multiple/>
					            </td>

							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
						<input type="checkbox" name="status" value="1" class="icheckbox_square-blue" > 有效
						<input type="checkbox" name="status" value="2" class="icheckbox_square-blue" > 无效
		      		</td>
		      	</tr>  	
			 </table>
			</form>
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="addContent()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

  </div>

<script>
	$('input').click(function () { var now = this; $('input').each(function (i, e) { if (now !== e) { $(e).attr('checked', false); } }) });

	$('#file').fileinput({
		language: 'zh',
		uploadUrl:'http://127.0.0.1:9099/file/upLoad',//文件上传路径
		browseClass: 'btn btn-primary',
		allowedFileExtensions: ["png","jpg","gif","txt"],
		enctype:"multipart/form-data",
	});
	$('#file').on('fileuploaded',function (event,data,previewId,index){
		//给image框值
		if (data.response!=null){
			$("#image").val(data.response.data);
		}
	})
	$(function () {
		listContent()
		listContentCategory()
	})
	function listContentCategory() {
		$.ajax({
			url:"/contentCategory/listContentCategory",
			data:{},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					var categorys = result.data
					var html = '<option value="-1">--请选择--</option>'
					$(categorys).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					$("#categorySelect").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listContentCategory2() {
		$.ajax({
			url:"/contentCategory/listContentCategory",
			data:{},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					var categorys = result.data
					var html = '<option value="-1">--请选择--</option>'
					$(categorys).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					$("#categorySelect2").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listContent() {
		$.ajax({
			url:"/content/listContent",
			data:{},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					contents = result.data
					var html = ''
					$(contents).each(function (i,e) {
						var status =''
						if (e.status == 1){
							status="有效"
						}else {
							status="无效"
						}
						html += '<tr>'
						html += '<td><input name="box" value="'+e.id+'" type="checkbox"></td>'
						html += '<td>'+e.id+'</td>'
						html += '<td>'+e.categoryId+'</td>'
						html += '<td>'+e.title+'</td>'
						html += '<td>'+e.url+'</td>'
						html += '<td>'
						html += '<img src="'+e.pic+'" width="100px" height="50px">'
						html += '</td>'
						html += '<td>'+e.sortOrder+'</td>'
						html += '<td>'+status+'</td>'
						html += '<td class="text-center">'
						html += '<button type="button" class="btn bg-olive btn-xs" onclick="updateContent('+e.id+')" data-toggle="modal" data-target="#updateModal">修改</button>'
						html += '</td>'
						html += '</tr>'
					})
					$("#contentBody").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function addContent() {
		$.ajax({
			url:"/content/addContent",
			data:$("#addForm").serialize(),
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function updateContent(id) {
		$.ajax({
			url:"/content/toUpdate",
			data:{id:id},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					var category = result.data
					var html =''

					html+='<div class="modal-dialog" >'
					html+='<div class="modal-content">'
					html+='<div class="modal-header">'
					html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
					html+='<h3 id="myModalLabel">广告编辑</h3>'
					html+='</div>'
					html+='<div class="modal-body">'
					html+='<form id="updateForm">'
					html+='<input type="hidden" name="id" value="'+category.id+'">'
					html+='<table class="table table-bordered table-striped"  width="800px">'
					html+='<tr>'
					html+='<td>广告分类</td>'
					html+='<td>'
					html+='<select class="form-control" name="categoryId" id="categorySelect2">'
					html+='</select>'
					html+='</td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>标题</td>'
					html+='<td><input value="'+category.title+'" class="form-control" name="title" placeholder="标题" >  </td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>URL</td>'
					html+='<td><input value="'+category.url+'" class="form-control" name="url" placeholder="URL" >  </td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>排序</td>'
					html+='<td><input value="'+category.sortOrder+'" class="form-control" name="sortOrder" placeholder="排序" >  </td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>广告图片</td>'
					html+='<td>'
					html+='<table>'
					html+='<tr>'
					html+='<td>'
					html+='<input type="hidden" id="image2" value="'+category.pic+'" name="pic">'
					html+='<input type="file" id="file2" name="myFile" multiple/>'
					html+='<button class="btn btn-primary" type="button" >上传</button>'
					html+='</td>'
					html+='</tr>'
					html+='</table>'
					html+='</td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>是否有效</td>'
					html+='<td>'
					html+='<input id="status1" type="checkbox" name="status" value="1" class="icheckbox_square-blue" > 有效'
					html+='<input id="status2" type="checkbox" name="status" value="2" class="icheckbox_square-blue" > 无效'
					html+='</td>'
					html+='</tr>'
					html+='</table>'
					html+='</form>'
					html+='</div>'
					html+='<div class="modal-footer">'
					html+='<button class="btn btn-success" onclick="updateContentCategory()" data-dismiss="modal" aria-hidden="true">保存</button>'
					html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
					html+='</div>'
					html+='</div>'
					html+='</div>'

					$("#updateModal").html(html)
					//给下拉框值
					listContentCategory2()
					//调用文件上传函数
					file(category.pic)

					//判断是否有效
					if (category.status == 1){
						$("#status1").attr("checked","checked")
					}else {
						$("#status2").attr("checked","checked")
					}

					//改变下拉框里面的值
					var categoryId=[category.categoryId]
					$("#categorySelect2").val(categoryId).trigger("change")

					$('input').click(function () { var now = this; $('input').each(function (i, e) { if (now !== e) { $(e).attr('checked', false); } }) });

				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function file(pic){
		$('#file2').fileinput({
			language: 'zh',
			uploadUrl:'http://127.0.0.1:9099/file/upLoad',//文件上传路径
			browseClass: 'btn btn-primary',
			allowedFileExtensions: ["png","jpg","gif","txt"],
			enctype:"multipart/form-data",
			initialPreviewAsData: true,//是否初始预览图片
			initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
			initialPreview:pic//图片回显(初始预览图片地址)
		});
		$('#file2').on('fileuploaded',function (event,data,previewId,index){
			//给image框值
			if (data.response!=null){
				$("#image2").val(data.response.data);
			}
		})
	}
	function updateContentCategory() {
		$.ajax({
			url:"/content/updateContent",
			data:$("#updateForm").serialize(),
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function delContent() {
		var boxs = document.getElementsByName("box");
		var ids = '';
		$(boxs).each(function (i,e) {
			if (e.checked){
				ids+= ","+e.value
			}
		})
		ids = ids.substring(1,ids.length)
		$.ajax({
			url:"/content/delContent",
			data:{ids:ids},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
</script>
</body>

</html>